package cn.edu.sdjzu.xg2121.service;

import cn.edu.sdjzu.xg2121.dao.SchoolDao;
import cn.edu.sdjzu.xg2121.dao.TeacherDao;
import cn.edu.sdjzu.xg2121.domain.School;
import cn.edu.sdjzu.xg2121.domain.Teacher;
import cn.edu.sdjzu.xg2121.domain.University;
import cn.edu.sdjzu.xg2121.util.Condition;
import cn.edu.sdjzu.xg2121.util.JdbcHelper;
import cn.edu.sdjzu.xg2121.util.Pagination;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Collection;
import java.util.List;

import static cn.edu.sdjzu.xg2121.service.SchoolService.schoolDao;


public final class SchoolService {
    public static SchoolService schoolService = new SchoolService();
    public static SchoolDao schoolDao = new SchoolDao();
    public int count(University university) throws SQLException {
        return schoolDao.count(university);
    }

    public School add(School school) throws SQLException {
        schoolDao.add(school);
        return school;
    }

    public boolean delete(int id) throws SQLException {
        Connection connection = JdbcHelper.getConn();
        return schoolDao.delete(id);
    }

    public String update(School school) throws SQLException {

        Connection connection = JdbcHelper.getConn();

        try {
            schoolDao.update(school);
            System.out.println("修改成功");
        } catch (SQLException e) {
            System.out.println("修改失败");
            e.printStackTrace();
        } finally {
            connection.setAutoCommit(true);
            JdbcHelper.close(null, connection);
        }
        return null;
    }

    public Collection<School> findAll(List<Condition> conditionList, Pagination pagination) throws SQLException {
        Connection connection = JdbcHelper.getConn();
        Collection<School> schools = schoolDao.findAll(connection, conditionList, pagination);
        JdbcHelper.close(connection);
        return schools;
    }
    public School find(Integer id) throws SQLException {
        School desiredSchools = null;
        Connection connection = JdbcHelper.getConn();
        String sql_select_id = "SELECT * FROM school WHERE id = ?";
        PreparedStatement pstmt_select_id = connection.prepareStatement(sql_select_id);
        pstmt_select_id.setLong(1,id);
        ResultSet resultSet = pstmt_select_id.executeQuery();
        if (resultSet.next()){
            int id3 = resultSet.getInt("id");
            String description = resultSet.getString("description");
            String no = resultSet.getString("no");
            String remarks = resultSet.getString("remarks");
            desiredSchools = new School(id3,description,no,remarks);
        }
        JdbcHelper.close(resultSet,pstmt_select_id,connection);
        return desiredSchools;
    }
    public static SchoolService getInstance() {
        return schoolService;
    }

    public School find(int university_id) throws SQLException {
        return SchoolDao.getInstance().find(university_id);
    }
}
